package com.bainarm.library_public_utils;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;


public class PublicUtils {

    private static final String TAG = "PublicUtils";

    // 获取系统时间
    public static String getCurrentDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdf.format(new Date());
    }

    // 获取设备SN码 TODO
    public static String getSnCode() {

        return "xxxxx";
    }

    // 获取CPU温度 TODO
    public static float getCpuTemperature(){

        float temperature = 0.0f;

        Log.d(TAG, "getCpuTemperature:" + temperature);

        return  temperature;

    }
    // 获取CPU占用率 TODO
    public static float getCpuUsage() {

        return 0.0f;  // 如果无法获取CPU占用率，则返回0.0
    }

    // 获取内存占用比例 TODO
    public static float getMemoryUsage() {
        Runtime runtime = Runtime.getRuntime();
        long totalMemory = runtime.totalMemory();  // 获取总内存
        long freeMemory = runtime.freeMemory();    // 获取空闲内存
        long usedMemory = totalMemory - freeMemory; // 计算已使用内存
        float memoryUsage = (float)usedMemory / totalMemory; // 计算内存占用比例

        Log.d(TAG, "Memory Usage: " + memoryUsage); // 输出内存占用比例

        return Float.parseFloat(String.format("%.4f", memoryUsage));  // 返回内存占用比例
    }

    // 连网状态 TODO
    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean status =  activeNetwork != null && activeNetwork.isConnectedOrConnecting();
        Log.d(TAG, "isNetworkConnected: " + status); // 输出内存连网状态
        return  status;
    }


}